From 5617c89c0f56a5b26aab1ceeb8d7b21dfa54439f Mon Sep 17 00:00:00 2001
From: Daniel Rosser <danoli3@gmail.com>
Date: Wed, 4 Feb 2015 12:24:13 +1100
Subject: [PATCH] Patch

---
 Include/tesselator.h | 14 ++++++++++++--
 Source/geom.c        | 17 ++++++++++++-----
 Source/mesh.c        |  8 +++++---
 Source/priorityq.c   | 17 ++++++++---------
 Source/sweep.c       | 47 +++++++++++++++++++++++------------------------
 Source/tess.c        | 19 +++++++++----------
 6 files changed, 69 insertions(+), 53 deletions(-)

diff --git a/Include/tesselator.h b/Include/tesselator.h
index c27541e..37429db 100755
--- a/Include/tesselator.h
+++ b/Include/tesselator.h
@@ -38,6 +38,10 @@ extern "C" {
 
 // See OpenGL Red Book for description of the winding rules
 // http://www.glprogramming.com/red/chapter11.html
+#if defined( __APPLE_CC__)
+	#include <TargetConditionals.h>
+#endif
+
 enum TessWindingRule
 {
 	TESS_WINDING_ODD,
@@ -115,11 +119,17 @@ enum TessElementType
 };
 
 typedef float TESSreal;
-typedef int TESSindex;
+//note this shouldn't be defined(TARGET_OS_IPHONE) as its always defined either 0 or 1
+#if TARGET_OS_IPHONE || ANDROID || __ARMEL__
+typedef unsigned short TESSindex;
+#else
+typedef unsigned int TESSindex;
+#endif
+
 typedef struct TESStesselator TESStesselator;
 typedef struct TESSalloc TESSalloc;
 
-#define TESS_UNDEF (~(TESSindex)0)
+#define TESS_UNDEF ((TESSindex)(~(TESSindex)0))
 
 #define TESS_NOTUSED(v) do { (void)(1 ? (void)0 : ( (void)(v) ) ); } while(0)
 
diff --git a/Source/geom.c b/Source/geom.c
index a49ea17..e22076b 100755
--- a/Source/geom.c
+++ b/Source/geom.c
@@ -30,7 +30,6 @@
 */
 
 //#include "tesos.h"
-#include <assert.h>
 #include "mesh.h"
 #include "geom.h"
 
@@ -55,7 +54,9 @@ TESSreal tesedgeEval( TESSvertex *u, TESSvertex *v, TESSvertex *w )
 	*/
 	TESSreal gapL, gapR;
 
-	assert( VertLeq( u, v ) && VertLeq( v, w ));
+	if(! VertLeq( u, v ) && VertLeq( v, w )){
+		return 0;
+	}
 
 	gapL = v->s - u->s;
 	gapR = w->s - v->s;
@@ -79,7 +80,9 @@ TESSreal tesedgeSign( TESSvertex *u, TESSvertex *v, TESSvertex *w )
 	*/
 	TESSreal gapL, gapR;
 
-	assert( VertLeq( u, v ) && VertLeq( v, w ));
+	if( !VertLeq( u, v ) && VertLeq( v, w )){
+		return 0;
+	}
 
 	gapL = v->s - u->s;
 	gapR = w->s - v->s;
@@ -110,7 +113,9 @@ TESSreal testransEval( TESSvertex *u, TESSvertex *v, TESSvertex *w )
 	*/
 	TESSreal gapL, gapR;
 
-	assert( TransLeq( u, v ) && TransLeq( v, w ));
+	if(! TransLeq( u, v ) && TransLeq( v, w )){
+		return 0;
+	}
 
 	gapL = v->t - u->t;
 	gapR = w->t - v->t;
@@ -134,7 +139,9 @@ TESSreal testransSign( TESSvertex *u, TESSvertex *v, TESSvertex *w )
 	*/
 	TESSreal gapL, gapR;
 
-	assert( TransLeq( u, v ) && TransLeq( v, w ));
+	if(! TransLeq( u, v ) && TransLeq( v, w )){
+		return 0;
+	}
 
 	gapL = v->t - u->t;
 	gapR = w->t - v->t;
diff --git a/Source/mesh.c b/Source/mesh.c
index 06a8ced..6b59074 100755
--- a/Source/mesh.c
+++ b/Source/mesh.c
@@ -122,7 +122,7 @@ static void MakeVertex( TESSvertex *newVertex,
 	TESSvertex *vPrev;
 	TESSvertex *vNew = newVertex;
 
-	assert(vNew != NULL);
+	if(vNew == NULL) return;
 
 	/* insert in circular doubly-linked list before vNext */
 	vPrev = vNext->prev;
@@ -154,7 +154,7 @@ static void MakeFace( TESSface *newFace, TESShalfEdge *eOrig, TESSface *fNext )
 	TESSface *fPrev;
 	TESSface *fNew = newFace;
 
-	assert(fNew != NULL); 
+	if(fNew == NULL) return;
 
 	/* insert in circular doubly-linked list before fNext */
 	fPrev = fNext->prev;
@@ -760,7 +760,9 @@ void tessMeshDeleteMesh( TESSalloc* alloc, TESSmesh *mesh )
 	while( fHead->next != fHead ) {
 		tessMeshZapFace( fHead->next );
 	}
-	assert( mesh->vHead.next == &mesh->vHead );
+	if( mesh->vHead.next != &mesh->vHead ){
+		return;
+	}
 
 	alloc->memfree( alloc->userData, mesh );
 }
diff --git a/Source/priorityq.c b/Source/priorityq.c
index 62a6654..a614a8a 100755
--- a/Source/priorityq.c
+++ b/Source/priorityq.c
@@ -31,7 +31,6 @@
 
 //#include "tesos.h"
 #include <stddef.h>
-#include <assert.h>
 #include "../Include/tesselator.h"
 #include "priorityq.h"
 
@@ -138,7 +137,7 @@ static void FloatDown( PriorityQHeap *pq, int curr )
 				++child;
 		}
 
-		assert(child <= pq->max);
+		if(child > pq->max) continue;
 
 		hChild = n[child].handle;
 		if( child > pq->size || LEQ( h[hCurr].key, h[hChild].key )) {
@@ -237,7 +236,7 @@ PQhandle pqHeapInsert( TESSalloc* alloc, PriorityQHeap *pq, PQkey keyNew )
 	if( pq->initialized ) {
 		FloatUp( pq, curr );
 	}
-	assert(free != INV_HANDLE);
+	//assert(free != INV_HANDLE);
 	return free;
 }
 
@@ -271,7 +270,7 @@ void pqHeapDelete( PriorityQHeap *pq, PQhandle hCurr )
 	PQhandleElem *h = pq->handles;
 	int curr;
 
-	assert( hCurr >= 1 && hCurr <= pq->max && h[hCurr].key != NULL );
+	if(!( hCurr >= 1 && hCurr <= pq->max && h[hCurr].key != NULL )) return;
 
 	curr = h[hCurr].node;
 	n[curr].handle = n[pq->size].handle;
@@ -324,7 +323,7 @@ PriorityQ *pqNewPriorityQ( TESSalloc* alloc, int size, int (*leq)(PQkey key1, PQ
 /* really tessPqSortDeletePriorityQ */
 void pqDeletePriorityQ( TESSalloc* alloc, PriorityQ *pq )
 {
-	assert(pq != NULL); 
+	if(pq == NULL) return;
 	if (pq->heap != NULL) pqHeapDeletePriorityQ( alloc, pq->heap );
 	if (pq->order != NULL) alloc->memfree( alloc->userData, pq->order );
 	if (pq->keys != NULL) alloc->memfree( alloc->userData, pq->keys );
@@ -410,7 +409,7 @@ int pqInit( TESSalloc* alloc, PriorityQ *pq )
 	p = pq->order;
 	r = p + pq->size - 1;
 	for( i = p; i < r; ++i ) {
-		assert( LEQ( **(i+1), **i ));
+		if(! LEQ( **(i+1), **i )) return 0;
 	}
 #endif
 
@@ -445,8 +444,8 @@ PQhandle pqInsert( TESSalloc* alloc, PriorityQ *pq, PQkey keyNew )
 			}
 		}
 	}
-	assert(curr != INV_HANDLE); 
-	pq->keys[curr] = keyNew;
+	if(curr != INV_HANDLE)
+		pq->keys[curr] = keyNew;
 
 	/* Negative handles index the sorted array. */
 	return -(curr+1);
@@ -505,7 +504,7 @@ void pqDelete( PriorityQ *pq, PQhandle curr )
 		return;
 	}
 	curr = -(curr+1);
-	assert( curr < pq->max && pq->keys[curr] != NULL );
+	if(!( curr < pq->max && pq->keys[curr] != NULL )) return;
 
 	pq->keys[curr] = NULL;
 	while( pq->size > 0 && *(pq->order[pq->size-1]) == NULL ) {
diff --git a/Source/sweep.c b/Source/sweep.c
index 112f1ef..6dfded2 100755
--- a/Source/sweep.c
+++ b/Source/sweep.c
@@ -29,7 +29,6 @@
 ** Author: Eric Veach, July 1994.
 */
 
-#include <assert.h>
 #include <stddef.h>
 #include <setjmp.h>		/* longjmp */
 
@@ -143,7 +142,7 @@ static void DeleteRegion( TESStesselator *tess, ActiveRegion *reg )
 		* deleted with zero winding number (ie. it better not get merged
 		* with a real edge).
 		*/
-		assert( reg->eUp->winding == 0 );
+		if( reg->eUp->winding != 0 ) return;
 	}
 	reg->eUp->activeRegion = NULL;
 	dictDelete( tess->dict, reg->nodeUp );
@@ -156,7 +155,7 @@ static int FixUpperEdge( TESStesselator *tess, ActiveRegion *reg, TESShalfEdge *
 * Replace an upper edge which needs fixing (see ConnectRightVertex).
 */
 {
-	assert( reg->fixUpperEdge );
+	if(! reg->fixUpperEdge ) return 0;
 	if ( !tessMeshDelete( tess->mesh, reg->eUp ) ) return 0;
 	reg->fixUpperEdge = FALSE;
 	reg->eUp = newEdge;
@@ -237,7 +236,7 @@ static int IsWindingInside( TESStesselator *tess, int n )
 			return (n >= 2) || (n <= -2);
 	}
 	/*LINTED*/
-	assert( FALSE );
+	//assert( FALSE );
 	/*NOTREACHED*/
 
 	return( FALSE );
@@ -346,7 +345,7 @@ static void AddRightEdges( TESStesselator *tess, ActiveRegion *regUp,
 	/* Insert the new right-going edges in the dictionary */
 	e = eFirst;
 	do {
-		assert( VertLeq( e->Org, e->Dst ));
+		if(! VertLeq( e->Org, e->Dst )) return;
 		AddRegionBelow( tess, regUp, e->Sym );
 		e = e->Onext;
 	} while ( e != eLast );
@@ -388,7 +387,7 @@ static void AddRightEdges( TESStesselator *tess, ActiveRegion *regUp,
 		ePrev = e;
 	}
 	regPrev->dirty = TRUE;
-	assert( regPrev->windingNumber - e->winding == reg->windingNumber );
+	if(!( regPrev->windingNumber - e->winding == reg->windingNumber )) return;
 
 	if( cleanUp ) {
 		/* Check for intersections between newly adjacent edges. */
@@ -528,7 +527,7 @@ static int CheckForLeftSplice( TESStesselator *tess, ActiveRegion *regUp )
 	TESShalfEdge *eLo = regLo->eUp;
 	TESShalfEdge *e;
 
-	assert( ! VertEq( eUp->Dst, eLo->Dst ));
+	if( VertEq( eUp->Dst, eLo->Dst )) return FALSE;
 
 	if( VertLeq( eUp->Dst, eLo->Dst )) {
 		if( EdgeSign( eUp->Dst, eLo->Dst, eUp->Org ) < 0 ) return FALSE;
@@ -575,11 +574,11 @@ static int CheckForIntersect( TESStesselator *tess, ActiveRegion *regUp )
 	TESSvertex isect, *orgMin;
 	TESShalfEdge *e;
 
-	assert( ! VertEq( dstLo, dstUp ));
-	assert( EdgeSign( dstUp, tess->event, orgUp ) <= 0 );
-	assert( EdgeSign( dstLo, tess->event, orgLo ) >= 0 );
-	assert( orgUp != tess->event && orgLo != tess->event );
-	assert( ! regUp->fixUpperEdge && ! regLo->fixUpperEdge );
+	if( VertEq( dstLo, dstUp )) return FALSE;
+	if( EdgeSign( dstUp, tess->event, orgUp ) > 0 ) return FALSE;
+	if( EdgeSign( dstLo, tess->event, orgLo ) < 0 ) return FALSE;
+	if(!( orgUp != tess->event && orgLo != tess->event )) return FALSE;
+	if(!( ! regUp->fixUpperEdge && ! regLo->fixUpperEdge )) return FALSE;
 
 	if( orgUp == orgLo ) return FALSE;	/* right endpoints are the same */
 
@@ -598,10 +597,10 @@ static int CheckForIntersect( TESStesselator *tess, ActiveRegion *regUp )
 
 	tesedgeIntersect( dstUp, orgUp, dstLo, orgLo, &isect );
 	/* The following properties are guaranteed: */
-	assert( MIN( orgUp->t, dstUp->t ) <= isect.t );
-	assert( isect.t <= MAX( orgLo->t, dstLo->t ));
-	assert( MIN( dstLo->s, dstUp->s ) <= isect.s );
-	assert( isect.s <= MAX( orgLo->s, orgUp->s ));
+	if(!( MIN( orgUp->t, dstUp->t ) <= isect.t )) return FALSE;
+	if(!( isect.t <= MAX( orgLo->t, dstLo->t ))) return FALSE;
+	if(!( MIN( dstLo->s, dstUp->s ) <= isect.s )) return FALSE;
+	if(!( isect.s <= MAX( orgLo->s, orgUp->s ))) return FALSE;
 
 	if( VertLeq( &isect, tess->event )) {
 		/* The intersection point lies slightly to the left of the sweep line,
@@ -904,7 +903,7 @@ static void ConnectLeftDegenerate( TESStesselator *tess,
 		/* e->Org is an unprocessed vertex - just combine them, and wait
 		* for e->Org to be pulled from the queue
 		*/
-		assert( TOLERANCE_NONZERO );
+		if(! TOLERANCE_NONZERO ) return;
 		SpliceMergeVertices( tess, e, vEvent->anEdge );
 		return;
 	}
@@ -925,7 +924,7 @@ static void ConnectLeftDegenerate( TESStesselator *tess,
 	/* vEvent coincides with e->Dst, which has already been processed.
 	* Splice in the additional right-going edges.
 	*/
-	assert( TOLERANCE_NONZERO );
+	if(! TOLERANCE_NONZERO ) return;
 	regUp = TopRightRegion( regUp );
 	reg = RegionBelow( regUp );
 	eTopRight = reg->eUp->Sym;
@@ -934,7 +933,7 @@ static void ConnectLeftDegenerate( TESStesselator *tess,
 		/* Here e->Dst has only a single fixable edge going right.
 		* We can delete it since now we have some real right-going edges.
 		*/
-		assert( eTopLeft != eTopRight );   /* there are some left edges too */
+		if( eTopLeft == eTopRight ) return;   /* there are some left edges too */
 		DeleteRegion( tess, reg );
 		if ( !tessMeshDelete( tess->mesh, eTopRight ) ) longjmp(tess->env,1);
 		eTopRight = eTopLeft->Oprev;
@@ -1145,10 +1144,10 @@ static void DoneEdgeDict( TESStesselator *tess )
 		* created by ConnectRightVertex().
 		*/
 		if( ! reg->sentinel ) {
-			assert( reg->fixUpperEdge );
-			assert( ++fixedEdges == 1 );
+			if(! reg->fixUpperEdge ) continue;
+			if( ++fixedEdges != 1 ) continue;
 		}
-		assert( reg->windingNumber == 0 );
+		if( reg->windingNumber != 0 ) continue;
 		DeleteRegion( tess, reg );
 		/*    tessMeshDelete( reg->eUp );*/
 	}
@@ -1255,7 +1254,7 @@ static int RemoveDegenerateFaces( TESStesselator *tess, TESSmesh *mesh )
 	for( f = mesh->fHead.next; f != &mesh->fHead; f = fNext ) {
 		fNext = f->next;
 		e = f->anEdge;
-		assert( e->Lnext != e );
+		if( e->Lnext == e ) continue;
 
 		if( e->Lnext->Lnext == e ) {
 			/* A face with only two edges */
@@ -1319,7 +1318,7 @@ int tessComputeInterior( TESStesselator *tess )
 	DonePriorityQ( tess );
 
 	if ( !RemoveDegenerateFaces( tess, tess->mesh ) ) return 0;
-	tessMeshCheckMesh( tess->mesh );
+	//tessMeshCheckMesh( tess->mesh );
 
 	return 1;
 }
diff --git a/Source/tess.c b/Source/tess.c
index 67c038f..81e12e1 100755
--- a/Source/tess.c
+++ b/Source/tess.c
@@ -30,7 +30,6 @@
 */
 
 #include <stddef.h>
-#include <assert.h>
 #include <setjmp.h>
 #include "bucketalloc.h"
 #include "tess.h"
@@ -51,7 +50,7 @@ static void Normalize( TESSreal v[3] )
 {
 	TESSreal len = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
 
-	assert( len > 0 );
+	if( len <= 0 ) return;
 	len = sqrtf( len );
 	v[0] /= len;
 	v[1] /= len;
@@ -328,7 +327,7 @@ int tessMeshTessellateMonoRegion( TESSmesh *mesh, TESSface *face )
 	* be close to the edge we want.
 	*/
 	up = face->anEdge;
-	assert( up->Lnext != up && up->Lnext->Lnext != up );
+	if(!( up->Lnext != up && up->Lnext->Lnext != up )) return 1;
 
 	for( ; VertLeq( up->Dst, up->Org ); up = up->Lprev )
 		;
@@ -364,7 +363,7 @@ int tessMeshTessellateMonoRegion( TESSmesh *mesh, TESSface *face )
 	/* Now lo->Org == up->Dst == the leftmost vertex.  The remaining region
 	* can be tessellated in a fan from this leftmost vertex.
 	*/
-	assert( lo->Lnext != up );
+	if( lo->Lnext == up ) return 1;
 	while( lo->Lnext->Lnext != up ) {
 		TESShalfEdge *tempHalfEdge= tessMeshConnect( mesh, lo->Lnext, lo );
 		if (tempHalfEdge == NULL) return 0;
@@ -582,9 +581,9 @@ void OutputPolymesh( TESStesselator *tess, TESSmesh *mesh, int elementType, int
 	TESSvertex* v = 0;
 	TESSface* f = 0;
 	TESShalfEdge* edge = 0;
-	int maxFaceCount = 0;
-	int maxVertexCount = 0;
-	int faceVerts, i;
+	TESSindex maxFaceCount = 0;
+	TESSindex maxVertexCount = 0;
+	TESSindex faceVerts, i;
 	TESSindex *elements = 0;
 	TESSreal *vert;
 
@@ -624,7 +623,7 @@ void OutputPolymesh( TESStesselator *tess, TESSmesh *mesh, int elementType, int
 		}
 		while (edge != f->anEdge);
 		
-		assert( faceVerts <= polySize );
+		if(faceVerts > polySize ) continue;
 
 		f->n = maxFaceCount;
 		++maxFaceCount;
@@ -938,7 +937,7 @@ int tessTesselate( TESStesselator *tess, int windingRule, int elementType,
 	}
 	if (rc == 0) longjmp(tess->env,1);  /* could've used a label */
 
-	tessMeshCheckMesh( mesh );
+	//tessMeshCheckMesh( mesh );
 
 	if (elementType == TESS_BOUNDARY_CONTOURS) {
 		OutputContours( tess, mesh, vertexSize );     /* output contours */
@@ -976,7 +975,7 @@ int tessGetElementCount( TESStesselator *tess )
 	return tess->elementCount;
 }
 
-const int* tessGetElements( TESStesselator *tess )
+const TESSindex* tessGetElements( TESStesselator *tess )
 {
 	return tess->elements;
 }
-- 
1.9.3 (Apple Git-50)

